// 提供登录功能，缺乏：
// 1.通过浏览器保存的JWT，直接进入首页
// 2.获得账号、密码，进行登录     --- 完成
// 3.登录跳转界面
// 4.将返回的JWT，存储到本地浏览器
var login = document.getElementById("login");
var phoneData = document.getElementById("phone");
var codeData = document.getElementById("code");
var getCode = document.getElementById("getCode");
// var med = process.env.VUE_APP_httpMethod;
// alert(process.env.VUE_APP_httpMethod);
// var reque = med+"/phone/generate";

// 获得验证码
getCode.onclick = function(){
    var requestBody = new FormData();
    requestBody.append('phone', phoneData.value);
    let object = {};

    requestBody.forEach((value, key) => (object[key] = value));
    let jsonData = JSON.stringify(object);

    // fetch("http://1.94.147.186:8000/fdData/listAll", options).then(response => {
    fetch("http://127.0.0.1:8000/phone/generate", 
    {
        headers: {
            'content-Type': 'application/json',
        },
        method:'post',
        body:jsonData
    }).then(response => {   
        return response.json();
    }).then(data => {
        if(data.code == 0){
            alert("不存在此用户，请输入正确的手机号");
        }else{
            alert(data.msg);
            // 验证码倒计时
            var time = 59;
            // 禁用按钮
            getCode.disabled = true;
            // 开启定时器
            var timer = setInterval(function () {
                // 判断剩余秒数
                if (time == 0) {
                    // 清除定时器和复原按钮
                    clearInterval(timer);
                    getCode.disabled = false;
                    getCode.value = '发送验证码';
                } else {
                    getCode.value = `${time}秒后重新获取`;
                    time--;
                }
            }, 1000);
            }
    });
}

// 登录
login.onclick = function(){
    var requestBody = new FormData();
    requestBody.append('phone', phoneData.value);
    requestBody.append('code', codeData.value);
    let object = {};

    requestBody.forEach((value, key) => (object[key] = value));
    let jsonData = JSON.stringify(object);
    
    // options.body = JSON.stringify(body);
    
    // fetch("http://1.94.147.186:8000/fdData/listAll", options).then(response => {
    fetch("http://127.0.0.1:8000/phone/codeLogin", 
    {
        headers: {
            'content-Type': 'application/json',
        },
        method:'post',
        body:jsonData
    }).then(response => {   
        return response.json();
    })
    .then(data => {
        //保存信息到本地，里面都token
        localStorage.setItem("project_b526_JWT",JSON.stringify(data.jwt));
        window.location.href = "/index.html";
        // showData(data);
    });

    // 展示数据,这是反馈数据的表单
    function showData(data) {
        var tb = document.getElementById("table_body");
        tb.innerHTML="";
        for (var i = 0; i < data.length; i++) {
            const row = document.createElement('tr');
            const date = new Date(data[i].fdTime);
            const formattedDate = date.toLocaleString(); // 使用本地格式
            row.innerHTML = `
                <td>${data[i].fdId}</td>
                <td>${data[i].levelName}</td>
                <td>${data[i].publicName}</td>
                <td>${data[i].provinceName}</td>
                <td>${data[i].cityName}</td>
                <td>${formattedDate}</td>
                <td>${data[i].fdDescribe}</td>
                <td>${data[i].fdAddress}</td>
                <td>${data[i].fdState}</td>
            `;
            tb.appendChild(row);
        };
        // Toast({
        //     message: "加入购物车成功",
        //     position: 'center',
        //     duration: 1000
        // });
    }
}